﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;
using LOG;

namespace DBAccess
{
   public class Common
    {
       public MySqlConnection getConnection()
       {
           try
           {
               string constr = ConfigurationManager.ConnectionStrings[""].ToString();
               return new MySqlConnection(constr);
           }
           catch (MySqlException ex)
           {
               Log.Add(ex, -999);
               return null;
           }
           catch (Exception ex)
           {
               Log.Add(ex, -998);
               return null;
           }
       }
       public DataTable getDataTable(MySqlCommand cmd)
       {
           try
           {
               DataTable ret = new DataTable();
               cmd.Connection = getConnection();
               if (cmd.Connection == null)
               {
                   Log.Add("mysqlconnection获取为null,请查询.", -997);
                   return null;
               }
               ret.Load(cmd.ExecuteReader());
               return ret;

           }
           catch (MySqlException ex)
           {
               Log.Add(ex, -999);
               return null;
           }
           catch (Exception ex)
           {
               Log.Add(ex, -998);
               return null;
           }
          
       }
       public int executeCommandStr(MySqlCommand cmd)
       {
           try
           {
               using (MySqlConnection con=getConnection())
               {
                   if (con == null) return -1;
                   cmd.Connection = con;
                   con.Open();
                   return cmd.ExecuteNonQuery();
               }
           }
           catch (MySqlException ex)
           {
               Log.Add(ex, -999);
               return -1;
           }
           catch (Exception ex)
           {
               Log.Add(ex, -998);
               return -1;
           }
       }
       public bool executeCommandTran(MySqlConnection con, MySqlCommand cmd,ref int ID)
       {
           try
           {
               System.Data.SqlClient.SqlCommand c = new System.Data.SqlClient.SqlCommand();
               ID = 0;
               if (cmd.ExecuteNonQuery() > 0)
               {
                   if (cmd.CommandText.StartsWith("insert into"))
                   {
                       cmd.CommandText = "select @@IDENTITY";
                       cmd.Parameters.Clear();
                       ID = Convert.ToInt32(cmd.ExecuteScalar());
                   }
                   return true;
               }
               else
                   return false;
           }
           catch (MySqlException ex)
           {
               
               Log.Add(ex, -999);
               return false;
           }
           catch (Exception ex)
           {
               Log.Add(ex, -998);
               return false;
           }
       }

    }
}
